<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>websocket.html</title>
    <meta charset="UTF-8" />
    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html" charset="UTF-8">
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
    <!-- 独立css -->
</head>
<body>
<div>
    <p id="response"></p>
</div>
<!-- 独立JS -->
<script type="text/javascript" src="jquery.js" charset="utf-8"></script>
<script type="text/javascript" src="sockjs.min.js" charset="utf-8"></script>
<script type="text/javascript" src="stomp.min.js" charset="utf-8"></script>
</body>
<script>

    var stompClient = null;
    //加载完浏览器后  调用connect（），打开双通道
    $(function(){
        //打开双通道
        connect()
    })
    //强制关闭浏览器  调用websocket.close（）,进行正常关闭
    window.onunload = function() {
        disconnect()
    }
    function connect(){
        var userId=1;
        var socket = new SockJS('https://www.test.bizvane.cn/wechatEnterprise/endpointOyzc'); //连接SockJS的endpoint名称为"endpointOyzc"
        stompClient = Stomp.over(socket);//使用STMOP子协议的WebSocket客户端
        stompClient.connect({},function(frame){//连接WebSocket服务端
            console.log('Connected:' + frame);
            //通过stompClient.subscribe订阅/topic/getResponse 目标(destination)发送的消息
            stompClient.subscribe('/user/' + userId + '/queue/getResponse',function(response){
                console.log(response+"rrrrrr")
                var code=JSON.parse(response.body);
                showResponse(code)
            });
        });
    }
    //关闭双通道
    function disconnect(){
        if(stompClient != null) {
            stompClient.disconnect();
        }
        console.log("Disconnected");
    }
    function showResponse(message){
        var response = $("#response");
        response.append("<p>只有userID为"+message.userId+"的人才能收到</p>");
    }

</script>

</html>